<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 权限管理 </el-breadcrumb-item>
                <el-breadcrumb-item> 权限查询 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <el-select v-model="query.status" placeholder="状态" class="handle-select mr10">
                    <el-option key="1" label="全部状态" value=""></el-option>
                    <el-option key="2" label="正常" value="01"></el-option>
                    <el-option key="3" label="冻结" value="00"></el-option>
                </el-select>
                <el-select v-model="query.type" placeholder="类型" class="handle-select mr10">
                    <el-option key="99" label="全部类型" value=""></el-option>
                    <el-option v-for="(item, index) in types" :key="index" :label="item.name" :value="item.code"> </el-option>
                </el-select>
                <el-input v-model="query.name" placeholder="权限名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="warning" icon="el-icon-plus" @click="dialogFormVisible = true">添加权限</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column type="index" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="名称"></el-table-column>
                <el-table-column prop="type" label="类型" :formatter="formatType"></el-table-column>
                <el-table-column prop="description" label="描述"></el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <el-switch
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            :value="scope.row.status"
                            :active-value="'01'"
                        ></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="350px">
                    <template slot-scope="scope">
                        <el-button type="success" size="mini" icon="el-icon-view" @click="view(scope.row.id)">详情</el-button>
                        <el-button type="primary" size="mini" icon="el-icon-edit" @click="edit(scope.row.id)">编辑</el-button>
                        <el-button type="danger" size="mini" icon="el-icon-close" @click="del(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageNum"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>

            <el-dialog title="添加权限" :visible.sync="dialogFormVisible" width="500px">
                <el-form :model="form" ref="form" :rules="rules">
                    <el-form-item label="权限名称" :label-width="formLabelWidth" prop="name">
                        <el-input v-model="form.name" style="width: 250px"></el-input>
                    </el-form-item>
                    <el-form-item label="权限类型" prop="type" :label-width="formLabelWidth">
                        <el-select v-model="form.type" placeholder="类型">
                            <el-option v-for="(item, index) in types" :key="index" :label="item.name" :value="item.code"> </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="状态" :label-width="formLabelWidth">
                        <el-switch
                            v-model="form.status"
                            :active-value="'01'"
                            active-text="开启"
                            inactive-text="关闭"
                            :inactive-value="'00'"
                        ></el-switch>
                    </el-form-item>

                    <el-form-item label="描述" :label-width="formLabelWidth">
                        <el-input type="textarea" rows="5" v-model="form.description" style="width: 350px"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="closeDialog">取 消</el-button>
                    <el-button type="primary" @click="addPermission('form')">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
export default {
    name: 'basetable',
    inject: ['reload'],
    data() {
        return {
            formLabelWidth: '80px',
            dialogFormVisible: false,
            query: {
                name: '',
                type: '',
                status: '',
                pageNum: 1,
                pageSize: 5
            },
            types: [],
            tableData: [],
            multipleSelection: [],
            delList: [],
            pageTotal: 0,
            form: {
                name: '',
                type: '',
                status: '01',
                description: ''
            },
            idx: -1,
            id: -1,
            rules: {
                name: [
                    { required: true, message: '请输入权限名称', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
                type: [{ required: true, message: '请选择权限类型', trigger: 'blur' }]
            }
        };
    },
    created() {
        this.getType();
        this.init();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        async init() {
            var d = await this.$http.post('/permissions', this.query);
            if (d.code == 200) {
                if (d.data != null) {
                    this.tableData = d.data.list;
                    this.pageTotal = d.data.total;
                }
            } else {
                this.$message.error(d.message);
            }
        },
        formatType(row, column, cellValue, index) {
            switch (cellValue) {
                case '00':
                    return '添加';
                case '01':
                    return '删除';
                case '02':
                    return '更新';
                case '03':
                    return '查询';
                default:
                    return '未知';
            }
        },
        getType() {
            this.types = JSON.parse(window.sessionStorage.getItem('type'));
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.init();
        },
        // 编辑操作
        view(id) {
            this.$router.push({ path: '/getPermission', query: { id: id } });
        },

        // 编辑操作
        edit(id) {
            this.$router.push({ path: '/updatePermission', query: { id: id } });
        },
        // 删除操作
        del(id) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(async () => {
                    var d = await this.$http.delete('/permission/' + id);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.reload();
                    } else {
                        this.$message.error(d.message);
                    }
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //批量删除
        async delAllSelection() {
            const length = this.multipleSelection.length;
            var delName = '';
            for (let i = 0; i < length; i++) {
                this.delList.push(this.multipleSelection[i].id);
                delName = delName + this.multipleSelection[i].name + ' ';
            }
            this.$confirm('确定要删除 ' + delName + '吗？', '提示', {
                type: 'warning'
            })
                .then(async () => {
                    var d = await this.$http.post('/delPermissions', this.delList);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.multipleSelection = [];
                        this.reload();
                    } else {
                        this.$message.error(d.message);
                    }
                })
                .catch(() => {});
        },
        // 分页导航
        handlePageChange(val) {
            this.query.pageNum = val;
            this.init();
        },
        //关闭假期框
        closeDialog() {
            this.form = {
                name: '',
                type: '',
                status: '01',
                description: ''
            };
            this.dialogFormVisible = false;
        },

        //添加假期
        addPermission(formname) {
            this.$refs[formname].validate(async (valid) => {
                if (valid) {
                    var d = await this.$http.post('/permission', this.form);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.reload();
                    } else {
                        this.$message.error(d.message);
                    }
                } else {
                    return false;
                }
            });
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
